<?php
namespace app\admin\controller;

use app\admin\BaseController;
use think\facade\Cache;
use think\facade\Validate;
use think\facade\Db;

use app\admin\model\Admin;
use app\admin\model\AdminMenu;
use app\admin\model\AdminRole;
use app\admin\model\AdminRoleMenu;

/* 
 * 角色管理
 */
class RoleController extends BaseController
{
    
    /*
     * 角色列表
     * author xfwed
     * date 2022.03.24 15:39 
     * ------------------------------------------
     * update phper  2022-03-24 07:10 将token记入缓存
    */
    public function roleList()
    {
        $list = AdminRole::where('status',1)
                ->select();
        return $this->ajaxReturn(100, '查询成功', $list);
    }
    
     /*
     * 添加角色
     * author xfwed
     * date 2022.03.24 14:49
     * ------------------------------------------
     * update phper  2022-03-24 07:10 xxxxxxxxx
    */
    public function addRole()
    {
        $all = input('param.');
        $validate = Validate::rule([
            'role|角色名称' => 'require',
            //'type|角色分类' => 'require',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, 'default');
        }
        
        $data['role'] = $all['role'];
        //$data['type'] = $all['type'];
        $data['description'] = input('param.description');
        
        $add = AdminRole::insert($data);
        
        return $this->ajaxReturn(100, '添加成功', 'default');
    }
    
    /*
     * 编辑角色
     * author xfwed
     * date 2022.03.24 15:58
     * ------------------------------------------
     * update phper  2022-03-24 07:10 xxxxxxxxx
    */
    public function saveRole()
    {
        $all = input('param.');
        $validate = Validate::rule([
            'id|id' => 'require',
            'role|角色名称' => 'require',
            //'type|角色分类' => 'require',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, 'default');
        }
        
        $data['role'] = $all['role'];
        //$data['type'] = $all['type'];
        $data['description'] = input('param.description');
        $data['update_time'] = gD();
        
        $save = AdminRole::where('id',$all['id'])->update($data);
        if($save){
            return $this->ajaxReturn(100, '修改成功', 'default');
        }  else {
            return $this->ajaxReturn(3, '修改失败', 'default');
        }
        
    }
    
    /*
     * 删除角色
     * author xfwed
     * date 2022.03.24 15:58
     * ------------------------------------------
     * update phper  2022-03-24 07:10 xxxxxxxxx
    */
    public function delRole()
    {
        $all = input('param.');
        $validate = Validate::rule([
            'id|id' => 'require',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, 'default');
        }
        
        $row = Admin::where('role_id',$all['id'])->where('status',1)->find();
        if($row){
            return $this->ajaxReturn(3, '请先删除绑定账号', 'default');
        }
        
        $data['status'] = 2;
        $data['update_time'] = gD();
        
        $save = AdminRole::where('id',$all['id'])->update($data);
        if($save){
            return $this->ajaxReturn(100, '删除成功', 'default');
        }  else {
            return $this->ajaxReturn(3, '删除失败', 'default');
        }
        
    }
    
    /*
     * 菜单权限
     * author xfwed
     * date 2022.03.24 16:22
     * ------------------------------------------
     * update phper  2022-03-24 07:10 xxxxxxxxx
    */
    public function roleMenu()
    {
        $all = input('param.');
        $validate = Validate::rule([
            'role_id|角色id' => 'require',
            'type|类型' => 'require',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, 'default');
        }
        
        if($all['type'] == 1){//1修改前回显数据  2修改数据
            $list = AdminMenu::getMenu($all['role_id']);
            return $this->ajaxReturn(100, '查询成功', $list);
        }  else {
            $menu_id = explode(',', input('param.menu_id'));
        
            Db::startTrans();
            //先删除该角色现有的权限
            $row = AdminRoleMenu::where('role_id',$all['role_id'])->find();
            if($row){
                $del = AdminRoleMenu::where('role_id',$all['role_id'])->delete();
            }  else {
                $del = true;
            }

            //添加新的权限
            for($i=0;$i<count($menu_id);$i++){
                $alldata[$i]['role_id'] = $all['role_id'];
                $alldata[$i]['menu_id'] = $menu_id[$i];
            }
            $add = AdminRoleMenu::insertAll($alldata);
            if($del && $add){
                Db::commit();
                return $this->ajaxReturn(100, '更新成功', 'default');
            }  else {
                Db::rollback();
                return $this->ajaxReturn(100, '更新失败', 'default');
            }
        }
        
        
    }
    
}
